// page-safe/home.js
const app = getApp()

Page({
  data: {
    date: {
      selectBuyDate: "2018-01-01",
      selectStartDate: "2018-01-01",
      selectEndDate: "2018-01-01",
    },
    dateList: [
      '请选择投保时长',
      '1 年',
      '2 年',
      '3 年',
      '4 年',
      '5 年',

    ],
    indexTime: 0,



    otherList: [
      '0',
      '10万',
      '20万',
      '30万',
      '40万',
      '50万',
    ],
    otherList1: [
      0,
      100000,
      200000,
      300000,
      400000,
      500000,
    ],
    indexOperation: 0,
    indexOther: 0,

    price: 0,
    feilv0: 0.70,
    feilv1: 0.35,
    feilv2: 1.00,
    feilv3: 0.20,
    feilv4: 0.14,
    feilv5: 0.05,
    feilv6: 0.20,

    price0: 0,
    price1: 0,
    price2: 0,
    price3: 0,
    price4: 0,
    price5: 0,
    price6: 0,

    allprice: 0,
    checked1: false,
    checked2: false,
    checked3: false,
    checked4: false,
    checked5: false,
    checked6: false,

    applicantName: '请输入投保人姓名',
    applicantId: '请输入投保人身份证号',
    insurantName: '请输入被保险人姓名',
    insurantPhone: '请输入被保险人联系方式',

    beneficiaryName: '请输入第一受益人姓名',
    beneficiaryName: '请输入发票抬头',
    beneficiaryName: '请输入设备品牌',


    address: '请输入保单邮寄地址',
    remark: '请输入您的备注信息',



    sfzqm: app.globalData.submitImage[0],
    sfzbm: app.globalData.submitImage[1],
    hegezheng: app.globalData.submitImage[2],
    zhengchesfz: app.globalData.submitImage[5],
    zhengqianfang: app.globalData.submitImage[6],
    zuoqianfang: app.globalData.submitImage[7],
    youqianfang: app.globalData.submitImage[8],
    youhoufang: app.globalData.submitImage[9],
    zuohoufang: app.globalData.submitImage[10],
    namePlate: app.globalData.submitImage[11],
    engineNameplate: app.globalData.submitImage[13],


  },
  submit: function (e) {

    var price
    if (e.detail.value.price != '') {
      price = e.detail.value.price
    } else {
      price = wx.getStorageSync('price');
    }



    var time = e.detail.value.time
    var operation = e.detail.value.operation
    var switch1 = e.detail.value.switch1

    var other = e.detail.value.other
    var switch2 = e.detail.value.switch2

    var switch3 = e.detail.value.switch3
    var switch4 = e.detail.value.switch4
    var switch5 = e.detail.value.switch5
    var switch6 = e.detail.value.switch6

    var applicantName
    if (e.detail.value.applicantName != '') {
      applicantName = e.detail.value.applicantName
    } else {
      applicantName = wx.getStorageSync('applicantName');
    }

    var applicantId
    if (e.detail.value.applicantId != '') {
      applicantId = e.detail.value.applicantId
    } else {
      applicantId = wx.getStorageSync('applicantId');
    }

    var insurantName
    if (e.detail.value.insurantName != '') {
      insurantName = e.detail.value.insurantName
    } else {
      insurantName = wx.getStorageSync('insurantName');
    }

    var insurantPhone
    if (e.detail.value.insurantPhone != '') {
      insurantPhone = e.detail.value.insurantPhone
    } else {
      insurantPhone = wx.getStorageSync('insurantPhone');
    }

    var beneficiaryName
    if (e.detail.value.beneficiaryName != '') {
      beneficiaryName = e.detail.value.beneficiaryName
    } else {
      beneficiaryName = wx.getStorageSync('beneficiaryName');
    }

    var invoiceTitle
    if (e.detail.value.invoiceTitle != '') {
      invoiceTitle = e.detail.value.invoiceTitle
    } else {
      invoiceTitle = wx.getStorageSync('invoiceTitle');
    }

    var deviceBrand
    if (e.detail.value.deviceBrand != '') {
      deviceBrand = e.detail.value.deviceBrand
    } else {
      deviceBrand = wx.getStorageSync('deviceBrand');
    }

    var namePlate = e.detail.value.namePlate
    var engineNameplate = e.detail.value.engineNameplate

    var address
    if (e.detail.value.address != '') {
      address = e.detail.value.address
    } else {
      address = wx.getStorageSync('address');
    }

    var remark
    if (e.detail.value.remark != '') {
      remark = e.detail.value.remark
    } else {
      remark = wx.getStorageSync('remark');
    }

    var buydata = e.detail.value.buydata

    if (price === '') {
      wx.showToast({
        title: '请输入设备价值',
        icon: 'loading'
      })
    } else if (this.data.allprice === 0) {
      wx.showToast({
        title: '请选择投保类型',
        icon: 'loading'
      })
    } else if (applicantName === '') {
      wx.showToast({
        title: '请输入投保人姓名',
        icon: 'loading'
      })
    } else if (applicantId === '') {
      wx.showToast({
        title: '请输入投保人身份证号',
        icon: 'loading'
      })
    } else if (applicantId.length != 18) {
      wx.showToast({
        title: '请输入正确的身份证号',
        icon: 'loading'
      })
    } else if (insurantName === '') {
      wx.showToast({
        title: '请输入被保险人姓名',
        icon: 'loading'
      })
    }
    else if (insurantPhone === '') {
      wx.showToast({
        title: '请输入被保险人手机号',
        icon: 'loading'
      })
    } else if (insurantPhone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'loading'
      })
    } else if (beneficiaryName === '') {
      wx.showToast({
        title: '请输入受益人姓名',
        icon: 'loading'
      })
    } else if (invoiceTitle === '') {
      wx.showToast({
        title: '请输入发票抬头',
        icon: 'loading'
      })
    } else if (buydata === '') {
      wx.showToast({
        title: '请输入购买日期',
        icon: 'loading'
      })
    } else if (namePlate === '') {
      wx.showToast({
        title: '请输入铭牌号',
        icon: 'loading'
      })
    } else if (engineNameplate === '') {
      wx.showToast({
        title: '请输入发动机号',
        icon: 'loading'
      })
    } else if (address === '') {
      wx.showToast({
        title: '请输入邮寄地址',
        icon: 'loading'
      })
    } else {
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      })
    }

    console.log(price);
    console.log(time);
    console.log(operation);

    console.log(other);

    console.log(switch1);
    console.log(switch2);
    console.log(switch3);
    console.log(switch4);
    console.log(switch5);
    console.log(switch6);



    console.log(applicantName);
    console.log(applicantId);
    console.log(insurantName);
    console.log(insurantPhone);
    console.log(beneficiaryName);
    console.log(invoiceTitle);
    console.log(buydata)
    console.log(deviceBrand);
    console.log(namePlate);
    console.log(engineNameplate);
    console.log(address);
    console.log(remark);
  },



  getInputNumber: function (e) {

    wx.setStorageSync('price', e.detail.value);
    this.setData({
      price: e.detail.value,
    });
    this.refresh()
  },
  timeChange: function (e) {
    wx.setStorageSync('indexTime', e.detail.value);
    this.setData({
      indexTime: e.detail.value
    });
    this.refresh()
  },

  otherChange: function (e) {
    wx.setStorageSync('indexOther', e.detail.value);
    this.setData({
      indexOther: e.detail.value,
    });
    this.refresh()
  },
  operationChange: function (e) {
    wx.setStorageSync('indexOperation', e.detail.value);
    this.setData({
      indexOperation: e.detail.value
    });
    this.refresh()
  },
  buyDateChange: function (e) {
    wx.setStorageSync('selectBuyDate', e.detail.value);
    this.setData({
      selectBuyDate: e.detail.value
    })
  },





  switchChange1: function (e) {

    this.setData({
      checked1: e.detail.value,
      indexOperation: 1,
    });
    this.refresh()
  },

  switchChange2: function (e) {

    this.setData({
      checked2: e.detail.value,
      indexOther: 1,
    });
    this.refresh()
  },
  switchChange3: function (e) {
    this.setData({
      checked3: e.detail.value,
    });
    this.refresh()
  },
  switchChange4: function (e) {
    this.setData({
      checked4: e.detail.value,
    });
    this.refresh()
  },
  switchChange5: function (e) {
    this.setData({
      checked5: e.detail.value,
    });
    this.refresh()
  },
  switchChange6: function (e) {
    this.setData({
      checked6: e.detail.value,
    });
    this.refresh()
  },
  switchChange7: function (e) {
    this.setData({
      checked7: e.detail.value,
    });
    this.refresh()
  },

  applicantName: function (e) {
    wx.setStorageSync('applicantName', e.detail.value);
    this.setData({
      applicantName: e.detail.value,
    });
  },
  applicantId: function (e) {
    wx.setStorageSync('applicantId', e.detail.value);
    this.setData({
      applicantId: e.detail.value,
    });
  },
  insurantName: function (e) {
    wx.setStorageSync('insurantName', e.detail.value);
    this.setData({
      insurantName: e.detail.value,
    });
  },
  insurantPhone: function (e) {
    wx.setStorageSync('insurantPhone', e.detail.value);
    this.setData({
      insurantPhone: e.detail.value,
    });
  },

  beneficiaryName: function (e) {
    wx.setStorageSync('beneficiaryName', e.detail.value);
    this.setData({
      beneficiaryName: e.detail.value,
    });
  },
  invoiceTitle: function (e) {
    wx.setStorageSync('invoiceTitle', e.detail.value);
    this.setData({
      invoiceTitle: e.detail.value,
    });
  },
  deviceBrand: function (e) {
    wx.setStorageSync('deviceBrand', e.detail.value);
    this.setData({
      deviceBrand: e.detail.value,
    });
  },

  namePlate: function (e) {
    wx.setStorageSync('namePlate', e.detail.value);
    this.setData({
      namePlate: e.detail.value,
    });
  },
  engineNameplate: function (e) {
    wx.setStorageSync('engineNameplate', e.detail.value);
    this.setData({
      engineNameplate: e.detail.value,
    });
  },
  address: function (e) {
    wx.setStorageSync('address', e.detail.value);
    this.setData({
      address: e.detail.value,
    });
  },
  remark: function (e) {
    wx.setStorageSync('remark', e.detail.value);
    this.setData({
      remark: e.detail.value,
    });
  },



  onLoad: function (options) {
    var price = wx.getStorageSync('price');
    var indexTime = wx.getStorageSync('indexTime');
    var indexOther = wx.getStorageSync('indexOther');
    var indexOperation = wx.getStorageSync('indexOperation');
    var selectByDate = wx.getStorageSync('selectByDate');


    var applicantName = wx.getStorageSync('applicantName');
    var applicantId = wx.getStorageSync('applicantId');
    var insurantName = wx.getStorageSync('insurantName');
    var insurantPhone = wx.getStorageSync('insurantPhone');

    var beneficiaryName = wx.getStorageSync('beneficiaryName');
    var invoiceTitle = wx.getStorageSync('invoiceTitle');
    var deviceBrand = wx.getStorageSync('deviceBrand');


    var address = wx.getStorageSync('address');
    var remark = wx.getStorageSync('remark');

    console.log('niho' + price + selectByDate + indexTime + indexOther + indexOperation)
    this.setData({
      price: price,
      indexTime: indexTime,
      indexOther: indexOther,
      indexOperation: indexOperation,
      selectByDate: selectByDate,

      applicantName: applicantName,
      applicantId: applicantId,
      insurantName: insurantName,
      insurantPhone: insurantPhone,
      beneficiaryName: beneficiaryName,
      invoiceTitle: invoiceTitle,
      deviceBrand: deviceBrand,

      address: address,
      remark: remark,
    });
    this.refresh()
  },


  onReady: function () {

  },


  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res.tempFilePaths);
        app.globalData.submitImage[e.target.id] = res.tempFilePaths
        if (e.target.id === '0') {
          self.setData({
            sfzqm: res.tempFilePaths,
          })
        } else if (e.target.id === '1') {
          self.setData({
            sfzbm: res.tempFilePaths
          })
        } else if (e.target.id === '2') {
          self.setData({
            hegezheng: res.tempFilePaths
          })
        } else if (e.target.id === '5') {
          self.setData({
            zhengchesfz: res.tempFilePaths
          })
        } else if (e.target.id === '6') {
          self.setData({
            zhengqianfang: res.tempFilePaths
          })
        } else if (e.target.id === '7') {
          self.setData({
            zuoqianfang: res.tempFilePaths
          })
        } else if (e.target.id === '8') {
          self.setData({
            youqianfang: res.tempFilePaths
          })
        } else if (e.target.id === '9') {
          self.setData({
            youhoufang: res.tempFilePaths
          })
        } else if (e.target.id === '10') {
          self.setData({
            zuohoufang: res.tempFilePaths
          })
        } else if (e.target.id === '11') {
          self.setData({
            namePlate: res.tempFilePaths
          })
        } else if (e.target.id === '13') {
          self.setData({
            engineNameplate: res.tempFilePaths
          })
        }
      }
    });
  },
  refresh: function () {
    var pp00 = this.data.feilv0 * this.data.indexTime * this.data.price / 100;
    var pp11 = this.data.otherList1[this.data.indexOperation] * this.data.indexTime * this.data.feilv1 / 100;
    var pp22 = this.data.otherList1[this.data.indexOther] * this.data.indexTime * this.data.feilv2 / 100;

    var pp33 = this.data.feilv3 * this.data.indexTime * this.data.price / 100;
    var pp44 = this.data.feilv4 * this.data.indexTime * this.data.price / 100;
    var pp55 = this.data.feilv5 * this.data.indexTime * this.data.price / 100;
    var pp66 = this.data.feilv6 * this.data.indexTime * this.data.price / 100;

    var pp0 = parseFloat(pp00.toFixed(2));
    var pp1 = parseFloat(pp11.toFixed(2));
    var pp2 = parseFloat(pp22.toFixed(2));
    var pp3 = parseFloat(pp33.toFixed(2));
    var pp4 = parseFloat(pp44.toFixed(2));
    var pp5 = parseFloat(pp55.toFixed(2));
    var pp6 = parseFloat(pp66.toFixed(2));

    this.setData({
      price0: pp0,

    });


    if (this.data.checked1 === true) {
      this.setData({
        price1: pp1,
      })
    } else {
      this.setData({
        price1: 0,
        indexOperation: 0

      })
    };

    if (this.data.checked2 === true) {
      this.setData({
        price2: pp2,
      })
    } else if (this.data.checked2 === false) {
      this.setData({
        price2: 0,
        indexOther: 0,
      })
    };
    if (this.data.checked3 === true) {
      this.setData({
        price3: pp3,
      })
    } else {
      this.setData({
        price3: 0,
      })
    };
    if (this.data.checked4 === true) {
      this.setData({
        price4: pp4,
      })
    } else {
      this.setData({
        price4: 0,
      })
    };

    if (this.data.checked5 === true) {
      this.setData({
        price5: pp5,
      })
    } else {
      this.setData({
        price5: 0,
      })
    };

    if (this.data.checked6 === true) {
      this.setData({
        price6: pp6,
      })
    } else {
      this.setData({
        price6: 0,
      })
    };

    var allprice11 = this.data.price1 + this.data.price2 + this.data.price3 + this.data.price4 + this.data.price5 + this.data.price6 + this.data.price0;
    var allprice1 = parseFloat(allprice11.toFixed(2));
    this.setData({
      allprice: allprice1,
    })
  },





})